home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / misc / charmap / source / acl.c next >
C/C++ Source or Header  |  1999-11-30  |  8KB  |  283 lines

  1. /******************************************************************
  2. ** acl.c: Contient la gestion des requètes ASL, du clipboard, et **
  3. **        support à la locale.library. Écrit par T.Pierron.      **
  4. **        14-11-1999                                             **
  5. ******************************************************************/
  6.  
  7.  
  8. #include <Intuition/Intuition.H>
  9. #include <Intuition/IntuitionBase.H>
  10. #include <Libraries/Commodities.H>
  11. #include <Libraries/GadTools.H>
  12. #include <Libraries/ASL.H>
  13. #include <Dos/DosASL.H>
  14. #include <Exec/IO.H>
  15. #include <Devices/Clipboard.H>
  16. #include <Libraries/Locale.H>
  17.  
  18. #define    CATCOMP_STRINGS
  19. #define  CATCOMP_NUMBERS
  20. #include "cmap_strings.h"
  21.  
  22.  
  23. extern struct IntuitionBase *IntuitionBase;
  24. extern struct AslBase *AslBase;
  25. extern struct Window *window;
  26. struct FontRequester    *FR=NULL;
  27.  
  28. /* ASL Font requester tags: */
  29. ULONG FileTags[] = {
  30.     ASLFO_Window,NULL,
  31.     ASLFO_Screen,NULL,
  32.     ASLFO_SleepWindow,TRUE,
  33.     ASLFO_TitleText,(ULONG) MSG_ASLTITLE_STR,
  34.     ASLFO_MaxHeight,255,
  35.     TAG_DONE
  36. };
  37.  
  38. UBYTE *NoASL=MSG_NOASL_STR;
  39.  
  40. /* Try to open a ASL font requester: */
  41. struct TTextAttr *open_asl()
  42. {
  43.     FileTags[1] = (ULONG) window;
  44.     FileTags[3] = (ULONG) IntuitionBase->ActiveScreen;
  45.  
  46.     if(!FR)
  47.         if( !(FR = (void *) AllocAslRequest(ASL_FontRequest,NULL)) ) {
  48.             puts(NoASL);
  49.             return NULL;
  50.         }
  51.  
  52.     if( AslRequest((APTR) FR,FileTags) )
  53.         return &FR->fo_TAttr;
  54.     else
  55.         /* User hit cancel or close gadget! */
  56.         return NULL;
  57. }
  58.  
  59. struct IntuiText MsgTxt={ 0,0,JAM1,0,0,NULL,NULL,0};
  60. struct IntuiText Ack={ 0,0,JAM1,0,0,NULL,MSG_BUTTON1_STR,0};
  61. struct IntuiText Nac={ 0,0,JAM1,0,0,NULL,MSG_BUTTON2_STR,0};
  62.  
  63. BYTE avert(UBYTE *msg)
  64. {
  65.     MsgTxt.IText=msg;
  66.     return AutoRequest( window,&MsgTxt,AslBase?&Ack:NULL,&Nac,NULL,NULL,320,72 );
  67. }
  68.  
  69.  
  70. /****************************************************************
  71. ** Procédures d'entrées-sorties avec le Clipboard.device. Tiré **
  72. ** d'un exemple des RKM, modifié et optimisé par l'auteur.     **
  73. ****************************************************************/
  74.  
  75.  
  76. #define MAKE_ID(a,b,c,d) ((a<<24L) | (b<<16L) | (c<<8L) | d)
  77.  
  78. #define ID_FORM MAKE_ID('F','O','R','M')
  79. #define ID_FTXT MAKE_ID('F','T','X','T')
  80. #define ID_CHRS MAKE_ID('C','H','R','S')
  81.  
  82. /* prototypes */
  83. struct IOClipReq    *CBOpen            ( ULONG );
  84. void                    CBClose            (struct IOClipReq *);
  85. int                    CBWriteFTXT        (struct IOClipReq *, UBYTE *);
  86. int                    CBQueryFTXT        (struct IOClipReq *);
  87. void                    CBReadCHRS        (struct IOClipReq *, UBYTE *,WORD Max);
  88. void                    CBReadDone        (struct IOClipReq *);
  89.  
  90.  
  91. /* Try to open the clipboard: */
  92. struct IOClipReq *CBOpen(ULONG unit)
  93. {
  94.     struct MsgPort *mp;
  95.     struct IOStdReq *ior;
  96.  
  97.     if( (mp = (void *) CreatePort(0L,0L)) &&
  98.         (ior=(struct IOStdReq *)CreateExtIO(mp,sizeof(struct IOClipReq))) &&
  99.         (!OpenDevice("clipboard.device",unit,ior,0L)) )
  100.         return (struct IOClipReq *)ior;
  101.  
  102.     DeleteExtIO((struct IOStdReq *)ior);
  103.     DeletePort(mp);
  104.     return NULL;
  105. }
  106.  
  107. /* Try to close it: */
  108. void CBClose(struct IOClipReq *ior)
  109. {
  110.     struct MsgPort *mp = ior->io_Message.mn_ReplyPort;
  111.  
  112.     CloseDevice((struct IOStdReq *)ior);
  113.     DeleteExtIO((struct IOStdReq *)ior);
  114.     DeletePort(mp);
  115. }
  116.  
  117. #define WriteBuf(Buf,Val,Type)    *((Type *)Buf)++=Val
  118. UBYTE Buffer[71];
  119.  
  120. /* Write a string of text to the clipboard.device (max 50 chars!): */
  121. int CBWriteFTXT(struct IOClipReq *ior,UBYTE *string)
  122. {
  123.     UBYTE *p;
  124.     ULONG slen;
  125.     int success;
  126.  
  127.     if( (slen = strlen(string))==0 ) return FALSE;
  128.     slen += (slen & 1);                    /* pad if odd */
  129.  
  130.     /* initial set-up for Offset, Error, and ClipID */
  131.     ior->io_Offset = 0;
  132.     ior->io_Error  = 0;
  133.     ior->io_ClipID = 0;
  134.     p = Buffer;
  135.  
  136.     /* Create the IFF header information */
  137.     WriteBuf(p,ID_FORM,ULONG);            /* "FORM"             */
  138.     WriteBuf(p,slen+12,ULONG);            /* + "[size]FTXTCHRS" */
  139.     WriteBuf(p,ID_FTXT,ULONG);            /* "FTXT"             */
  140.     WriteBuf(p,ID_CHRS,ULONG);            /* "CHRS"             */
  141.     WriteBuf(p,slen,ULONG);                /* string length      */
  142.     CopyMem(string,p,slen);
  143.  
  144.     /* Write buffer to clipboard: */
  145.     ior->io_Data    = (STRPTR)Buffer;
  146.     ior->io_Length  = p-Buffer+slen;
  147.     ior->io_Command = CMD_WRITE;
  148.     DoIO( (struct IORequest *) ior);
  149.  
  150.     ior->io_Command=CMD_UPDATE;
  151.     DoIO( (struct IORequest *) ior);
  152.  
  153.     /* Check if io_Error was set by any of the preceding IO requests */
  154.     return ior->io_Error ? FALSE : TRUE;
  155. }
  156.  
  157. #define Tab(Buf,Type,n)        ((Type *)Buf)[n]
  158.  
  159. /* Check if there is TXT in the clipboard: */
  160. int CBQueryFTXT(struct IOClipReq *ior)
  161. {
  162.     /* initial set-up for Offset, Error, and ClipID */
  163.     ior->io_Offset = 0;
  164.     ior->io_Error  = 0;
  165.     ior->io_ClipID = 0;
  166.  
  167.     /* Look for "FORM[size]FTXT" */
  168.     ior->io_Command = CMD_READ;
  169.     ior->io_Data    = (STRPTR)Buffer;
  170.     ior->io_Length  = 12;
  171.     DoIO( (struct IORequest *) ior);
  172.  
  173.     if( (ior->io_Actual == 12L) &&                    /* Do we have at least 12 bytes ? */
  174.         (Tab(Buffer,ULONG,0) == ID_FORM) &&        /* Does it starts with "FORM" ? */
  175.         (Tab(Buffer,ULONG,2) == ID_FTXT) )            /* Is it "FTXT" ? */
  176.         return TRUE;
  177.  
  178.     /* It's not "FORM[size]FTXT", so tell clipboard we are done */
  179.     CBReadDone(ior);
  180.     return FALSE;
  181. }
  182.  
  183.  
  184. /* Reads the next CHRS chunk from clipboard */
  185. void CBReadCHRS(struct IOClipReq *ior,UBYTE *buf, WORD Max)
  186. {
  187.     ULONG size;
  188.  
  189.     /* Find next CHRS chunk */
  190.     ior->io_Command = CMD_READ;
  191.     ior->io_Data    = (STRPTR)buf;
  192.     ior->io_Length  = 8L;
  193.     for(;;)
  194.     {
  195.         /* Read the chunk ID and its length: */
  196.         DoIO( (struct IORequest *) ior);
  197.  
  198.         /* Have get enough data ? */
  199.         if (ior->io_Actual != 8) break;
  200.  
  201.         /* Get buffer size, and pad it if odd: */
  202.         size = Tab(Buffer,ULONG,1);
  203.         if (size & 1) size++;
  204.  
  205.         /* Is it a CHRS chunk ? */
  206.         if(Tab(buf,ULONG,0) == ID_CHRS)
  207.         {
  208.             if(size >= Max) size=Max-1;
  209.             ior->io_Length  = size;
  210.  
  211.             DoIO( (struct IOStdReq *) ior);
  212.             Buffer[size]    = 0;
  213.             break;
  214.         }
  215.         /* If not, skip to next chunk */
  216.         else
  217.             ior->io_Offset += size;
  218.     }
  219.     CBReadDone(ior);
  220. }
  221.  
  222. /* Tell clipboard we are done reading */
  223. void CBReadDone(struct IOClipReq *ior)
  224. {
  225.     ior->io_Command = CMD_READ;
  226.     ior->io_Data    = (STRPTR)Buffer;
  227.     ior->io_Length  = sizeof(Buffer)-2;
  228.  
  229.     /* falls through immediately if io_Actual == 0 */
  230.     while (ior->io_Actual)
  231.         if (DoIO( (struct IORequest *) ior)) break;
  232. }
  233.  
  234. /****************************************************************
  235. ** Locale.library support (50 lines !)                                    **
  236. ****************************************************************/
  237.  
  238.  
  239. /* All strings affected: */
  240. extern UBYTE *GadTxt[],*Errors[],*popmsg[],*NoMem;
  241. extern struct NewWindow new_window;
  242. extern struct NewMenu newmenu[];
  243. extern struct EasyStruct Request;
  244. extern struct NewBroker newbroker;
  245.  
  246. struct Vars {
  247.     UBYTE **msg;            /* Message to change */
  248.     UBYTE nb;                /* Nb contiguous msg to change */
  249.     WORD  size;                /* Size of contiguous memory */
  250.     WORD    NumStr;            /* Id string from catalog */
  251. } TabVars[]={
  252.     GadTxt,5,sizeof(UBYTE *),MSG_STRGAD,
  253.     Errors,6,sizeof(UBYTE *),MSG_BADOS,
  254.     popmsg,4,sizeof(UBYTE *),MSG_CODE,
  255.     &newmenu->nm_Label,16,sizeof(struct NewMenu),MSG_MENUTITLE,
  256.     &Request.es_Title,3,sizeof(UBYTE *),MSG_ABOUT,
  257.     &newbroker.nb_Name,3,sizeof(UBYTE *),MSG_COMMONAME,
  258.     (UBYTE **)&FileTags[7],1,0,MSG_ASLTITLE,
  259.     &new_window.Title,1,0,MSG_WINTITLE,
  260.     &NoMem,1,0,MSG_NOMEM,
  261.     &NoASL,1,0,MSG_NOASL,
  262.     &Ack.IText,1,0,MSG_BUTTON1,
  263.     &Nac.IText,1,0,MSG_BUTTON1,
  264. };
  265.  
  266. void *catalog=NULL;
  267.  
  268. /* Translate all messages of the application: */
  269. void Translate_srings()
  270. {
  271.     if(catalog=(void *)OpenCatalogA(NULL,"CharMap.catalog",NULL))
  272.     {
  273.         struct Vars *p;
  274.         WORD n;
  275.         UBYTE **mes;
  276.         /* All necessary information is contained in our table: */
  277.         for(p=TabVars; p<TabVars+sizeof(TabVars)/sizeof(struct Vars); p++)
  278.             for(n=0,mes=p->msg; n<p->nb; n++,((UBYTE *)mes) += p->size )
  279.                 if(*mes!=((STRPTR)-1))    /* Newmenus barlabel string */
  280.                     *mes = (UBYTE *) GetCatalogStr(catalog,p->NumStr++,*mes);
  281.     }
  282. }
  283.